package com.gkj.gulimall.product.service.impl;

import com.gkj.gulimall.common2.utils.PageUtils;
import com.gkj.gulimall.product.entity.CategoryEntity;
import com.gkj.gulimall.product.service.CategoryBrandRelationService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkj.common.utils.Query;

import com.gkj.gulimall.product.dao.CategoryDao;
import com.gkj.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    //    @Autowired
//    CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);

//        entities.stream().filter((categoryEntity)->{//查找所有的父类id=0的商品种类信息
//                    return categoryEntity.getParentCid() == 0;
//                }
//        ).collect(Collectors.toList());
//        组装成父子的树形结构
        List<CategoryEntity> level1Menus = entities.stream().filter((categoryEntity) ->{
                    return categoryEntity.getParentCid() == 0;
                }
        ).map((menu) -> {
            System.out.println(menu);
//            System.out.println(menu.getParentCid());
//            System.out.println(menu.getCatId());
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return entities;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        // TODO: 2022/1/24 1.检查当前删除的菜单，是否被别的地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        findParentPath(catelogId,paths);
        return paths.toArray(new Long[paths.size()]);
    }
    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    @Override
    public List<CategoryEntity> getLevel1Categorys() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        List<CategoryEntity> entities = categoryEntities.stream().filter((category) -> {
            return category.getParentCid() == 0;
        }).collect(Collectors.toList());
        return entities;
    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        //通过递归查找父类id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid() != 0){
            findParentPath(byId.getParentCid(),paths);
        }
        Collections.reverse(paths);
        return paths;
    }

    //三级菜单的遍历
    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
        return all.stream().filter((categoryEntity1)->{
            return categoryEntity1.getParentCid() == root.getCatId();//把一级菜单的所有对应的二级菜单遍历出来
        }).map((categoryEntity) -> {
            //找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            return (menu1.getSort() == null ? 0:menu1.getSort()) - (menu2.getSort() == null ? 0:menu2.getSort());
        }).collect(Collectors.toList());
    }

    public List<CategoryEntity> getChildren(){
        List<CategoryEntity> MenuOne = listWithTree();
        MenuOne.stream().filter((categoryEntity -> {
            return categoryEntity.getParentCid() == 0;
        })).collect(Collectors.toList());
        return MenuOne;
    }

}